﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using ByteArray;
using System.Diagnostics;

namespace ByteArray
{
    public class Detector : IDetector
    {
        private string _path;
        private Processor _processor;
        private string _outputPath;
        public Detector(SensitiveResearchOption option, Processor processor)
        {
            _path = option.InputPath;
            _outputPath = option.OutputPath;
            _processor = processor;
        }

        public void Start()
        {
            using (StreamReader sr = new StreamReader(_path))
            {
                StringBuilder TextBuilder = new StringBuilder();
                string line;
                string buffer = "";

                while ((line = sr.ReadLine()) != null)
                {
                    // 处理合并后的文本
                    buffer += line;
                    var (processedbuffer, processedtext) = _processor.Check(buffer);
                    buffer = processedbuffer;
                    TextBuilder.AppendLine(processedtext);   
                }
                TextBuilder.AppendLine(buffer);


                // 将处理后的文本写入输出文件
                using (var writer = new StreamWriter(_outputPath, false))
                {
                    writer.Write(TextBuilder.ToString());
                    Dictionary<string, int> wordCounts = _processor.GetWordCounts();
                    foreach (var wordCount in wordCounts)
                    {
                        writer.WriteLine($"{wordCount.Key} - {wordCount.Value} times");
                    }

                    //控制台输出
                    Console.WriteLine($"处理后的文本:{TextBuilder}");
                    Console.WriteLine("查找到的敏感词及出现次数");
                    foreach (var wordCount in wordCounts)
                    {
                        Console.WriteLine($" {wordCount.Key} - {wordCount.Value} times");
                    }
                }
            }
        }
    }
}



